package org.beetl.sql.gen.main;

import org.beetl.sql.core.*;
import org.beetl.sql.core.db.H2Style;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.gen.SourceBuilder;
import org.beetl.sql.gen.SourceConfig;
import org.beetl.sql.gen.simple.*;
import org.beetl.sql.gen.simple.controller.ControllerSourceBuilder;
import org.beetl.sql.gen.simple.controller.VoBuilder;
import org.beetl.sql.gen.simple.service.ImplSourceBuilder;
import org.beetl.sql.gen.simple.service.ServiceSourceBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Pompey_hpy
 * @Date: 2020/12/30
 * @Description:
 */
public class BeetlsqlGenCode {

    private static String driver ="com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://127.0.0.1:3306/flow_mysql?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false";
    private static String userName = "root";
    private static String password = "root";


    public static void beetlGenCode(String tableName) {

        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url,userName, password);
        //source是唯一必须的参数，其他参数都有默认值
        SQLManagerBuilder builder = new SQLManagerBuilder(source);
        //设置NameConversion，这里数据库命名采用下划线风格，使用UnderlinedNameConversion
        builder.setNc(new UnderlinedNameConversion());
        //设置一个拦截器，输出debug日志，包含了sql语句和执行参数，执行时间
        builder.setInters(new Interceptor[]{new DebugInterceptor()});
        //设置数据库分隔，必须跟数据库一样
        builder.setDbStyle(new H2Style());
        SQLManager sqlManager = builder.build();


        List<SourceBuilder> sourceBuilder = new ArrayList<>();
        //实体层
        SourceBuilder entityBuilder = new EntitySourceBuilder();
        //Mapper层
        SourceBuilder mapperBuilder = new MapperSourceBuilder();
        //md文件
        SourceBuilder mdBuilder = new MDSourceBuilder();
        //数据库markdown文档
        //SourceBuilder docBuilder = new MDDocBuilder();
        //Controller层
        SourceBuilder controllerBuilder = new ControllerSourceBuilder(tableName);
        //vo层
        SourceBuilder voBuilder = new VoBuilder(tableName);
        //service层
        SourceBuilder serviceSourceBuilder = new ServiceSourceBuilder(tableName);
        //impl层
        SourceBuilder implSourceBuilder = new ImplSourceBuilder(tableName);

        sourceBuilder.add(entityBuilder);
        sourceBuilder.add(mapperBuilder);
        sourceBuilder.add(mdBuilder);
        //sourceBuilder.add(docBuilder);
        sourceBuilder.add(controllerBuilder);
        sourceBuilder.add(voBuilder);
        sourceBuilder.add(serviceSourceBuilder);
        sourceBuilder.add(implSourceBuilder);

        SourceConfig config = new SourceConfig(sqlManager,sourceBuilder);
        //设置文件输出
        SimpleMavenProject project=new SimpleMavenProject("com.flowCloud.web");
        config.gen(tableName,project);

        //设置控制台输出
        //ConsoleOnlyProject consoleOnlyProject = new ConsoleOnlyProject();
        //config.gen(tableName,consoleOnlyProject);

        System.out.println(tableName+"已自动生成！");
    }

    public static void main(String[] args) {
        //根据表生成文件
        beetlGenCode("tbl_log");
    }
}
